package ch.ethz.inf.shop.model.simpleDatabase.operators.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import ch.ethz.inf.shop.model.simpleDatabase.Tuple;
import ch.ethz.inf.shop.model.simpleDatabase.operators.Scan;
import ch.ethz.inf.shop.model.simpleDatabase.operators.Select;
import ch.ethz.inf.shop.model.simpleDatabase.operators.TupleAccepter;

public class TestSelect {
	@Test
	public void selectRecipeItemTest() {
		// Method 1 to create an inline Interface implemenation, for Method 2
		// look further down...
		TupleAccepter tc = new TupleAccepter() {
			@Override
			public boolean acceptTuple(Tuple t) {
				return t.getInt("recipe_item.product") == 4;
			}
		};
		Select p = new Select(new Scan(AllTests.ROOT_DIRECTORY + "/recipe_item.txt", "recipe_item"), tc);

		int lineCnt = 0;
		while (p.moveNext()) {
			lineCnt++;
		}
		assertEquals(4, lineCnt);

		// ... Method 2 to create an inline interface implementation
		p = new Select(new Scan(AllTests.ROOT_DIRECTORY + "/recipe_item.txt", "recipe_item"), new TupleAccepter() {
			@Override
			public boolean acceptTuple(Tuple t) {
				return t.getInt("recipe_item.product") == 1;
			}
		});

		lineCnt = 0;
		while (p.moveNext()) {
			lineCnt++;
		}
		assertEquals(3, lineCnt);
	}

	@Test
	public void selectUserTest() {

		Select p = new Select(new Scan(AllTests.ROOT_DIRECTORY + "/registered_user.txt", "registered_user"), new TupleAccepter() {
			@Override
			public boolean acceptTuple(Tuple t) {
				return "rop".equals(t.getString("registered_user.name"));
			}
		});
		assertTrue(p.moveNext());

		assertEquals(1, p.current().getInt(0));
		assertEquals("rop", p.current().getString(1));
		assertEquals("rop", p.current().getString(2));
		assertEquals("philipp.rohr@gmail.com", p.current().getString(3));

		assertFalse(p.moveNext());
	}
}
